/* Remote call functionality */

/*
 *  Prepares parameter that will be sent to remote server. Spaces and plus signs
 *  are replaced so that they can be sent as HTTP parameters.
 *
 *  PARAMS:
 *      param       parameter to prepare
 *  RETURNS:
 *      the prepared parameter
 */
function prepareHTTPParam(param) {
    /* Check that given parameter is not a number */
    if (isNaN(param)) {
        param = escape(param);
    }
    return param;
}

/*
 * Calls a remote process acording the information given as a parameter.
 * This is a general function to call different actions that needs rcall
 * like validy check or after change event.
 * PARAMS:
 *  actionStr Information for rcall. It is in following format:
 *            res@host,port,dir/,script_name(par_1, par_2)
 *              res     Name of the field or variable that receives the result
 *                      There can be several result variables comma separated.
 *
 *              host,port,dir,script_name
 *                      Information where the remote script is located.
 *              par_n   Parameters closed in brackets. This is comma separated
 *                      list. Strings must be enclosed with '-marks
 *
 *  returnHandler A callback function to handle the return value.
 */
function doRCall(actionStr, returnHandler) {
    /* Split to two cells. 1) res 2) host,port,dir/,script_name(par_1, par_2) */
    aArr1 = actionStr.split("=");
    if (aArr1.length == 1)
        aArr1 = Array('', aArr1[0]);

    /* Split again two cells. 1) host,port,dir/,script_name 2) par_1, par_2) */
    aArr2 = aArr1[1].split("(");
    /* Remove the trailing bracket */
    aArr2[1] = aArr2[1].substr(0, aArr2[1].length-1);

    /* Now action string is split like follows:
        aArr1[0] = res
        aArr2[0] = script location information
        aArr2[1] = parameters
    */

    /* Split result variables into an array called result */
    if (aArr1[0] != '')
        results = aArr1[0].split(",");
    else
        results = new Array(0);

    /* Split parameters into an array named param */
    if (aArr1[1] != '')
        param = aArr2[1].split(",");
    else
        param = new Array(0);

    /* Split script location information  into an array named addr */
    addr = aArr2[0].split(",");

    rcall(addr[0],  // host
          addr[1],  // port
          addr[2],  // directory
          addr[3],  // script name
          results,
          param,
          returnHandler);
} // doRCall()


/*
 * Calls remote server. Uses sub window to deliver remote call to the server.
 * PARAMS:
 *  server      The name of the server that handles the request
 *  port        The port that the server listens to
 *  directory   The directory where the script resides
 *  script      The name of the script that handles to remote call
 *  results     List of widgets that receives an return value
 *  param       Array of parameters.
 *  rhandler    Name of the function that handles the result from the server.
 *              This function must have one parameter of array that contains
 *              the results strings aswell in format: <key>=<Value>
 */
function rcall(server, port, directory, script, results, rpc_params, rHandler) {
//    addrStr = 'HTTP://'+ server+':'+port+'/'+directory;

    /* Poirt is not used because Mozilla does not like it. */
    addrStr = 'HTTP://'+ server+'/';
    if (directory != '')
        addrStr = addrStr + directory + '/';

    manageStr = "";
    paramStr = 'rpc='+script+'&';
    resultStr = "";
    pCount = rpc_params.length;
    rCount = results.length;

    for (c = 0; c < pCount; c++) {
        s = "param" + c + "=" +prepareHTTPParam(eval(rpc_params[c])) + "&";
        paramStr = paramStr + s;
    }
    for (c = 0; c < rCount; c++) {
        s = "result" + c + "=" +prepareHTTPParam(results[c]) + "&";
        resultStr = resultStr + s;
    }
    /* Construct manage data parameters */
    manageStr = manageStr + "ResultHandler=" + rHandler + "&";
    manageStr = manageStr + "ParCount=" + pCount + "&";
    manageStr = manageStr + "ResCount=" + rCount;

    callStr = addrStr + "index.php?" + paramStr + resultStr + manageStr;
    NewWnd = window.open(callStr, 'rpcWindow', 'height=100,width=200');
}
